//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.authority.service.impl;

import com.farm.authority.dao.OutuserDaoInter;
import com.farm.authority.domain.Outuser;
import com.farm.authority.service.OutuserServiceInter;
import com.farm.authority.service.UserServiceInter;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.time.TimeTool;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OutuserServiceImpl implements OutuserServiceInter {
    @Resource
    private OutuserDaoInter outuserDaoImpl;
    @Resource
    private UserServiceInter userServiceImpl;
    private static final Logger log = Logger.getLogger(OutuserServiceImpl.class);

    public OutuserServiceImpl() {
    }

    @Transactional
    public Outuser insertOutuserEntity(Outuser entity, LoginUser user) {
        return this.outuserDaoImpl.insertEntity(entity);
    }

    @Transactional
    public Outuser editOutuserEntity(Outuser entity) {
        Outuser entity2 = this.outuserDaoImpl.getEntity(entity.getId());
        entity2.setAccountname(entity.getAccountname());
        entity2.setAccountid(entity.getAccountid());
        entity2.setUserid(entity.getUserid());
        entity2.setPcontent(entity.getPcontent());
        entity2.setPstate(entity.getPstate());
        entity2.setCtime(entity.getCtime());
        entity2.setId(entity.getId());
        this.outuserDaoImpl.editEntity(entity2);
        return entity2;
    }

    @Transactional
    public void deleteOutuserEntity(String id, LoginUser user) {
        this.outuserDaoImpl.deleteEntity(this.outuserDaoImpl.getEntity(id));
    }

    @Transactional
    public Outuser getOutuserEntity(String id) {
        return id == null ? null : this.outuserDaoImpl.getEntity(id);
    }

    @Transactional
    public DataQuery createOutuserSimpleQuery(DataQuery query) {
        DataQuery dbQuery = DataQuery.init(query, "ALONE_AUTH_OUTUSER a left join ALONE_AUTH_USER b on a.userid=b.id", "a.ID as ID,ACCOUNTNAME,ACCOUNTID,USERID,a.PCONTENT as PCONTENT,a.PSTATE as PSTATE,a.CTIME as CTIME,NAME");
        return dbQuery;
    }

    public OutuserDaoInter getOutuserDaoImpl() {
        return this.outuserDaoImpl;
    }

    public void setOutuserDaoImpl(OutuserDaoInter dao) {
        this.outuserDaoImpl = dao;
    }

    @Transactional
    public LoginUser getUserByOutUserId(String outuserid, String name, String content) {
        List<DBRule> rules = new ArrayList();
        rules.add(new DBRule("ACCOUNTID", outuserid, "="));
        List<Outuser> outusers = this.outuserDaoImpl.selectEntitys(rules);
        LoginUser user = null;
        Outuser outuser;
        if (outusers != null && outusers.size() > 0) {
            outuser = (Outuser)outusers.get(0);
            if (outuser.getUserid() != null && !outuser.getUserid().isEmpty()) {
                user = this.userServiceImpl.getUserEntity(outuser.getUserid());
            }
        } else {
            outuser = new Outuser();
            outuser.setAccountid(outuserid);
            outuser.setAccountname(name);
            outuser.setCtime(TimeTool.getTimeDate14());
            outuser.setPcontent(content);
            outuser.setPstate("1");
            this.insertOutuserEntity(outuser, (LoginUser)null);
        }

        return user;
    }

    @Transactional
    public Outuser getOutuserByOutUserid(String outuserid) {
        List<DBRule> rules = new ArrayList();
        rules.add(new DBRule("ACCOUNTID", outuserid, "="));
        List<Outuser> outusers = this.outuserDaoImpl.selectEntitys(rules);
        return outusers.size() > 0 ? (Outuser)outusers.get(0) : null;
    }
}
